package c.j.a.d.b.l;

import android.content.Context;
import android.text.TextUtils;
import c.j.a.d.b.e.b;
import c.j.a.d.b.e.i;
import c.j.a.d.b.n.c0;
import com.ss.android.socialbase.downloader.g.c;
import org.json.JSONArray;
import org.json.JSONObject;

/* compiled from: DownloadSetting */
public class a {
    public static final c0<Integer, a> e = new c0(8, 8);
    public static final a f = new a(null);
    public static JSONObject g;
    public static JSONObject h;
    public static Boolean i;
    public static boolean j;
    public static a k;
    public final JSONObject a;
    public final JSONObject b;
    public final Boolean c;
    public int d;

    static {
        h();
    }

    public a(JSONObject jSONObject) {
        Boolean bool;
        this.a = jSONObject;
        JSONObject jSONObject2 = null;
        if (jSONObject != null) {
            String str = "bugfix";
            if (!n(str)) {
                Boolean valueOf;
                jSONObject = jSONObject.optJSONObject(str);
                if (jSONObject != null) {
                    str = "default";
                    if (jSONObject.has(str) && !n(str)) {
                        boolean z = false;
                        if (jSONObject.optInt(str, 0) == 1) {
                            z = true;
                        }
                        valueOf = Boolean.valueOf(z);
                    }
                }
                Boolean bool2 = valueOf;
                jSONObject2 = jSONObject;
                bool = bool2;
                this.b = jSONObject2;
                this.c = bool;
            }
        }
        bool = null;
        this.b = jSONObject2;
        this.c = bool;
    }

    public static a d(int i) {
        a aVar = k;
        if (aVar != null && aVar.d == i) {
            return aVar;
        }
        a aVar2;
        c0 c0Var = e;
        synchronized (c0Var) {
            aVar2 = (a) c0Var.get(Integer.valueOf(i));
        }
        if (aVar2 == null) {
            if (j) {
                aVar2 = f;
            } else {
                Context a = b.a();
                if (a != null) {
                    c k = i.a(a).k(i);
                    if (k != null) {
                        aVar2 = e(k);
                    }
                }
                aVar2 = f;
            }
            synchronized (c0Var) {
                c0Var.put(Integer.valueOf(i), aVar2);
            }
        }
        aVar = aVar2;
        aVar.d = i;
        k = aVar;
        return aVar;
    }

    public static a e(c cVar) {
        if (j) {
            return f;
        }
        try {
            String g = cVar.g();
            if (!TextUtils.isEmpty(g)) {
                return new a(new JSONObject(g));
            }
        } catch (Throwable th) {
            th.printStackTrace();
        }
        return f;
    }

    public static a f(JSONObject jSONObject) {
        if (jSONObject == null || jSONObject == b.C() || j) {
            return f;
        }
        a aVar = k;
        if (aVar != null && aVar.a == jSONObject) {
            return aVar;
        }
        c0 c0Var = e;
        synchronized (c0Var) {
            for (a aVar2 : c0Var.values()) {
                if (aVar2.a == jSONObject) {
                    k = aVar2;
                    return aVar2;
                }
            }
            aVar = new a(jSONObject);
            k = aVar;
            return aVar;
        }
    }

    public static void h() {
        JSONObject C = b.C();
        boolean z = false;
        j = C.optInt("disable_task_setting", 0) == 1;
        g = C.optJSONObject("disabled_task_keys");
        C = C.optJSONObject("bugfix");
        Boolean bool = null;
        if (C != null) {
            String str = "default";
            if (C.has(str)) {
                if (C.optInt(str, 0) == 1) {
                    z = true;
                }
                bool = Boolean.valueOf(z);
            }
        }
        h = C;
        i = bool;
    }

    public static void i(int i, JSONObject jSONObject) {
        if (jSONObject != null && jSONObject != b.C() && !j) {
            c0 c0Var = e;
            synchronized (c0Var) {
                Object obj = k;
                if (obj == null || obj.a != jSONObject) {
                    obj = null;
                    for (a aVar : c0Var.values()) {
                        if (aVar.a == jSONObject) {
                            aVar.d = i;
                            obj = aVar;
                            break;
                        }
                    }
                    if (obj == null) {
                        obj = new a(jSONObject);
                        obj.d = i;
                    }
                    k = obj;
                } else {
                    obj.d = i;
                }
                e.put(Integer.valueOf(i), obj);
            }
        }
    }

    public static void j(int i) {
        a aVar = k;
        if (aVar != null && aVar.d == i) {
            k = null;
        }
        c0 c0Var = e;
        synchronized (c0Var) {
            c0Var.remove(Integer.valueOf(i));
        }
    }

    public static boolean n(String str) {
        JSONObject jSONObject = g;
        if (jSONObject == null || jSONObject.optInt(str, 0) != 1) {
            return false;
        }
        return true;
    }

    public double a(String str, double d) {
        JSONObject jSONObject = this.a;
        if (jSONObject == null || !jSONObject.has(str) || n(str)) {
            return b.C().optDouble(str, d);
        }
        return this.a.optDouble(str, d);
    }

    public int b(String str, int i) {
        JSONObject jSONObject = this.a;
        if (jSONObject == null || !jSONObject.has(str) || n(str)) {
            return b.C().optInt(str, i);
        }
        return this.a.optInt(str, i);
    }

    public long c(String str, long j) {
        JSONObject jSONObject = this.a;
        if (jSONObject == null || !jSONObject.has(str) || n(str)) {
            return b.C().optLong(str, j);
        }
        return this.a.optLong(str, j);
    }

    public String g(String str, String str2) {
        JSONObject jSONObject = this.a;
        if (jSONObject == null || !jSONObject.has(str) || n(str)) {
            return b.C().optString(str, str2);
        }
        return this.a.optString(str, str2);
    }

    public boolean k(String str, boolean z) {
        boolean z2 = false;
        if (!(this.b == null || n(str))) {
            if (this.b.has(str)) {
                if (this.b.optInt(str, z) == 1) {
                    z2 = true;
                }
                return z2;
            }
            Boolean bool = this.c;
            if (bool != null) {
                return bool.booleanValue();
            }
        }
        JSONObject jSONObject = h;
        if (jSONObject != null) {
            if (jSONObject.has(str)) {
                if (h.optInt(str, z) == 1) {
                    z2 = true;
                }
                return z2;
            }
            Boolean bool2 = i;
            if (bool2 != null) {
                return bool2.booleanValue();
            }
        }
        return z;
    }

    public JSONObject l(String str) {
        JSONObject jSONObject = this.a;
        if (jSONObject == null || !jSONObject.has(str) || n(str)) {
            return b.C().optJSONObject(str);
        }
        return this.a.optJSONObject(str);
    }

    public JSONArray m(String str) {
        JSONObject jSONObject = this.a;
        if (jSONObject == null || !jSONObject.has(str) || n(str)) {
            return b.C().optJSONArray(str);
        }
        return this.a.optJSONArray(str);
    }
}
